// 秒转换为时分秒
import {getCookie} from "@/util/CookieUtil.js";

export function secondToTime(s) {
    const h = parseInt(s / 3600)
    const minute = parseInt(s / 60 % 60)
    const second = Math.ceil(s % 60)

    const hours = h < 10 ? '0' + h : h
    const formatSecond = second > 59 ? 59 : second
    return `${hours > 0 ? `${hours}:` : ''}${minute < 10 ? '0' + minute : minute}:${formatSecond < 10 ? '0' + formatSecond : formatSecond}`
}
// 将timestamp转换为指定字符串格式
export function getTime(time, x) {
    const data = new Date(time);
    const year = data.getFullYear();
    const month = data.getMonth() + 1;
    const day = data.getDate();
    const hours = data.getHours();
    const minutes = data.getMinutes();
    const seconds = data.getSeconds();
    switch (x) {
        // YY/MM/DD
        case 1:{
            return year+"/"+month+"/"+day;
        }
        // YY/MM/DD HH:mm
        case 2: {
            return year+"/"+month+"/"+day+" "+hours+":"+minutes;
        }
        // YY-MM-DD HH:mm:SS
        case 10: {
            return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
        }
    }
}
//获取url值
export function getParam(key) {
    var url = window.location.href;
    var params = url.split('?')[1];
    if (params===undefined) return null;//地址无参
    console.log(params);
    var param = params.split('&');
    for(var i = 0; i < param.length; ++i) {
        var paramKeys = param[i].split('=');
        var paramKey = paramKeys[0];
        if(key===paramKey) {
            var value = '';
            for(var j = 1; j < paramKeys.length; ++j) {
                if(paramKeys[j]==='') value+='=';
                else value+=paramKeys[j];
            }
            return value;
        }
    }
    return;
}
// 获取token
export function getToken() {
    var cookie_token = getCookie('satoken');
    if(cookie_token===undefined||cookie_token==='') {
        return '';
    } else {
        return 'Bearer ' + cookie_token;
    }
}
